package com.wpml.cloud.myprovider.service;

import com.google.common.collect.Lists;
import com.wpml.cloud.myprovider.model.Person.Person;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Service
public class PersonService {

    static Map<Integer, Person> userMap = new ConcurrentHashMap<>();

    static {
        userMap.put(1, new Person(1, "黄忠"));
        userMap.put(2, new Person(2, "孙尚香"));
        userMap.put(3, new Person(3, "赵子龙"));
        userMap.put(4, new Person(4, "刘备"));
        userMap.put(5, new Person(5, "张飞"));
        userMap.put(6, new Person(6, "关羽"));
        userMap.put(7, new Person(7, "鲁班"));
        userMap.put(8, new Person(8, "李元芳"));
        userMap.put(9, new Person(9, "狄仁杰"));
        userMap.put(10, new Person(10, "百里守约"));
    }

    public Mono<Person> getById(Integer id) {
        return Mono.create(monoSink -> {
            System.out.println("service getById start");
            monoSink.success(getOne(id));
            System.out.println("service getById end");
        });
    }


    public Person getOne(Integer id) {
        System.out.println("service getOne start");
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Person person = userMap.get(id);
        System.out.println("service getOne end");
        return person;
    }

    public List<Person> getAll() {
        return Lists.newArrayList(userMap.values());
    }


}
